package cn.tedu.milktea.admin.controller;

import cn.tedu.milktea.admin.mapper.CartService;
import cn.tedu.milktea.admin.mapper.Milk;
import cn.tedu.milktea.common.web.JsonResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    // 将购物项添加到购物车
    @PostMapping("/delete")
    @ApiOperation("根据用户选择的购物车商品删除(支持批量)")
    @ApiImplicitParam(value = "删除购物车的id",name="ids",required = true,
            dataType = "array")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult removeCartsByIds(Long[] ids){
       CartService.removeCart(ids);
        return JsonResult.ok();
    }

    // 根据用户id清空购物车
    @PostMapping("/delete/all")
    @ApiOperation("根据用户id清空购物车")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult removeCartsByUserId(){
        CartService.removeAllCarts();;
        return JsonResult.ok("购物车已清空");
    }

}